package dao.impl;

import bean.dto.EquipmentPage;
import bean.pojo.ComplaintRecord;
import bean.pojo.Maintenance;
import commons.dbHelper.DBHelper;
import commons.jdbc.basic.JDBCUtil;
import dao.ComplaintRecordDao;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * dao.impl
 * User: xlh
 * Date: 2024/12/21 15:28
 * motto:推到高塔
 * Description:
 * Version: V1.0
 */
public class ComplaintRecordDaoImpl implements ComplaintRecordDao {
    private static DBHelper dbHelper=new DBHelper();
    @Override
    public List<ComplaintRecord> findByPage(EquipmentPage equipmentPage) {
        List<Object> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer("" +
                " select complaint_record_id, tel_phone,complaint_target,complaint_reason,processing_result," +
                "complaint_time,processing_personnel_name,start_processing_time,note" +
                " from complaint_record where 1=1 ");
        if (equipmentPage.getSelectMonth()!= null) {
            System.out.println(equipmentPage.getSelectMonth());
            sql.append(" and DATE_FORMAT(complaint_time, '%Y-%m') = DATE_FORMAT(STR_TO_DATE(?, '%Y-%m'), '%Y-%m') ");
            list.add(equipmentPage.getSelectMonth().trim());
        }
        if (equipmentPage.getSelectedStatus()!= null && equipmentPage.getSelectedStatus().trim().length() > 0) {
            sql.append(" and processing_result=? ");
            list.add(equipmentPage.getSelectedStatus().trim());
        }
        sql.append(" order by complaint_time desc ");
        sql.append(" limit " + (equipmentPage.getPage() - 1) * equipmentPage.getLimit() + ", " + equipmentPage.getLimit() );
        // 打印 SQL 语句用于调试，可根据实际情况选择保留或删除
        System.out.println("sql:" + sql);
        List<ComplaintRecord> beanList = dbHelper.getBeanList(ComplaintRecord.class, sql.toString(), list.toArray());
        System.out.println(beanList.toString());
        System.out.println(beanList.toArray());
        return beanList;

    }
    @Override
    public Long findByTotal(EquipmentPage equipmentPage) {
        StringBuffer   sql=new StringBuffer("select  count(*) ct  from  complaint_record  where  1=1 ");
        //集合参数
        List<Object>  parameters=new ArrayList<>();
        //验证是否为空  null  "    "
        if(equipmentPage.getSelectedStatus()!=null){
            parameters.add(equipmentPage.getSelectedStatus());
            sql.append(" and  processing_result like  concat('%',?,'%')");
        }
        //验证是否为空  null  "    "
        if(equipmentPage.getSelectMonth()!=null){
            parameters.add(equipmentPage.getSelectMonth());
            sql.append(" and  complaint_time like  concat('%',?,'%')");
        }
        System.out.println("sql:"+sql);
        System.out.println("参数:"+parameters);
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql.toString(), parameters.toArray());
        //默认0条
        Long  ct=0L;
        if(query.size()>0){
            //改变
            ct=(Long)query.get(0).get("ct");
        }
        return ct;
    }

    @Override
    public boolean updatComplaintRecord(ComplaintRecord complaintRecord) {
        String sql="update complaint_record set processing_result=?,processing_personnel_name=?," +
                "start_processing_time=?,note=? where complaint_record_id=?";
        complaintRecord.setStartProcessingTime(LocalDateTime.now());
        return dbHelper.update(sql,complaintRecord.getProcessingResult(),complaintRecord.getProcessingPersonnelName(),
                complaintRecord.getStartProcessingTime(), complaintRecord.getNote(),complaintRecord.getComplaintRecordId())>0;
    }

    @Override
    public boolean createComplaintRecord(ComplaintRecord complaintRecord) {
        String sql="insert complaint_record (" +
                "complaint_reason,complaint_time ,tel_phone,processing_result,complaint_target) values(?,?,?,?,?)";

        complaintRecord.setComplaintTime(LocalDateTime.now());
        complaintRecord.setProcessingResult("待处理");
        return dbHelper.update(sql,complaintRecord.getComplaintReason(),complaintRecord.getComplaintTime(),
                complaintRecord.getTelPhone(),complaintRecord.getProcessingResult(),complaintRecord.getComplaintTarget())>0;
    }
}
